using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CareerWinDialog : MonoBehaviour
{
	public UILabel titleLabel;

	public UILabel subtitleLabel;

	public UILabel textLabel;

	public UILabel coinsLabel;

	public UILabel starsLabel;

	public UILabel dominatedLabel;

	public UIWidget panel;

	public List<UISprite> stars = new List<UISprite>();

	public List<GameObject> objectsToHideWhenVisible = new List<GameObject>();

	[Header("Stars animations")]
	public float starAppearDuration = 0.2f;

	public float starStartScale = 1.2f;

	public float starDelayBetween = 0.5f;

	[Header("Slide in animation")]
	public Vector3 initialPanelOffset;

	public float slideInDuration;

	public float initialScale;

	public float initialAlpha;

	[Header("Prize animations")]
	public float prizeAppearDuration = 0.2f;

	public float prizeStartScale = 1.2f;

	public float prizeDelayBetween = 0.5f;

	[Header("Text animation")]
	public float textAppearDuration = 0.2f;

	public float textStartScale = 1.2f;

	public AnimationCurve easeCurve;

	private bool initialized_;

	protected MenuParameters.MenuParams.ShowScreen showScreen;

	private void Init()
	{
		if (!initialized_)
		{
			initialized_ = true;
			base.gameObject.SetActive(false);
		}
	}

	public void Awake()
	{
		Init();
	}

	public void Show(string title, string subtitle, string text, int activeStars, int coins, int newStars, MenuParameters.MenuParams.ShowScreen showScreen, TableTennisMatchController match)
	{
		this.showScreen = showScreen;
		Init();
		base.gameObject.SetActive(true);
		titleLabel.text = title;
		subtitleLabel.text = subtitle;
		textLabel.text = text;
		textLabel.cachedGameObject.SetActive(newStars == 0);
		coinsLabel.cachedGameObject.SetActive(false);
		coinsLabel.text = "+" + coins;
		starsLabel.cachedGameObject.SetActive(false);
		starsLabel.text = "+" + newStars;
		dominatedLabel.cachedGameObject.SetActive(false);
		foreach (UISprite star in stars)
		{
			SetStarActive(star, false);
		}
		StopAllCoroutines();
		StartCoroutine(ShowAnimation(activeStars, coins, newStars));
		foreach (GameObject item in objectsToHideWhenVisible)
		{
			UITools.SetActive(item, false);
		}
	}

	private IEnumerator ShowAnimation(int activeStars, int coins, int newStars)
	{
		Vector3 startPosition = initialPanelOffset;
		Vector3 endPosition = Vector3.zero;
		Vector3 startScale = initialScale * Vector3.one;
		Vector3 endScale = Vector3.one;
		float startAlpha = initialAlpha;
		int endAlpha = 1;
		panel.cachedTransform.localPosition = startPosition;
		float time = 0f;
		while (time < slideInDuration)
		{
			time += RealTime.deltaTime;
			float normTime = easeCurve.Evaluate(time / slideInDuration);
			panel.cachedTransform.localPosition = Vector3Ex.LerpNotClamped(startPosition, endPosition, normTime);
			panel.cachedTransform.localScale = Vector3Ex.LerpNotClamped(startScale, endScale, normTime);
			panel.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			yield return null;
		}
		panel.cachedTransform.localPosition = endPosition;
		panel.cachedTransform.localScale = endScale;
		panel.alpha = endAlpha;
		yield return new WaitForSeconds(0.25f);
		yield return StartCoroutine(ShowText(textLabel, textStartScale, textAppearDuration));
		yield return new WaitForSeconds(0.25f);
		yield return StartCoroutine(ShowStars(activeStars));
		yield return new WaitForSeconds(0.25f);
		if (newStars > 0)
		{
			yield return StartCoroutine(ShowPrize(starsLabel, newStars, prizeStartScale, prizeAppearDuration));
			yield return new WaitForSeconds(prizeDelayBetween);
		}
		if (coins > 0)
		{
			yield return StartCoroutine(ShowPrize(coinsLabel, coins, prizeStartScale, prizeAppearDuration));
			yield return new WaitForSeconds(prizeDelayBetween);
		}
	}

	private IEnumerator ShowText(UILabel prize, float scale, float duration)
	{
		prize.cachedGameObject.SetActive(true);
		Vector3 startScale = Vector3.one * scale;
		Vector3 endScale = Vector3.one;
		float startAlpha = 0f;
		float endAlpha = 1f;
		float time = 0f;
		prize.cachedTransform.localScale = startScale;
		prize.alpha = startAlpha;
		while (time < duration)
		{
			time += Time.deltaTime;
			float normTime = time / duration;
			prize.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, normTime);
			prize.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			yield return null;
		}
		prize.cachedTransform.localScale = endScale;
		prize.alpha = endAlpha;
	}

	private IEnumerator ShowPrize(UILabel prize, int prizeAmmount, float scale, float duration)
	{
		prize.cachedGameObject.SetActive(true);
		prize.text = "+0";
		Vector3 startScale = Vector3.one * scale;
		Vector3 endScale = Vector3.one;
		float startAlpha = 0f;
		float endAlpha = 1f;
		float time = 0f;
		prize.cachedTransform.localScale = startScale;
		prize.alpha = startAlpha;
		while (time < duration)
		{
			time += Time.deltaTime;
			float normTime = time / duration;
			prize.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, normTime);
			prize.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			prize.text = "+" + (int)Mathf.Lerp(0f, prizeAmmount, normTime);
			yield return null;
		}
		prize.cachedTransform.localScale = endScale;
		prize.alpha = endAlpha;
		prize.text = "+" + prizeAmmount;
	}

	private IEnumerator ShowStars(int numOfActiveStars)
	{
		foreach (UISprite star2 in stars)
		{
			UISprite uISprite = star2;
		}
		int starIndex = 0;
		foreach (UISprite star in stars)
		{
			SetStarActive(star, starIndex < numOfActiveStars);
			StartCoroutine(ShowStar(star));
			yield return new WaitForSeconds(starDelayBetween);
			starIndex++;
		}
		if (numOfActiveStars >= 3)
		{
			dominatedLabel.cachedGameObject.SetActive(true);
			yield return StartCoroutine(ShowStar(dominatedLabel));
		}
	}

	private void SetStarActive(UIWidget star, bool active)
	{
		UIStyledItem component = star.GetComponent<UIStyledItem>();
		if (component != null)
		{
			component.SetStyle((!active) ? "Star.NotActive" : "Star.Active");
		}
		else
		{
			star.color = ((!active) ? Color.black : Color.white);
		}
	}

	private IEnumerator ShowStar(UIWidget star)
	{
		Vector3 startScale = Vector3.one * starStartScale;
		Vector3 endScale = Vector3.one;
		star.cachedTransform.localScale = startScale;
		float startAlpha = 0f;
		float endAlpha = 1f;
		float time = 0f;
		star.alpha = startAlpha;
		while (time < starAppearDuration)
		{
			time += RealTime.deltaTime;
			float normTime = time / starAppearDuration;
			star.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, normTime);
			star.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			yield return null;
		}
		star.cachedTransform.localScale = endScale;
		star.alpha = endAlpha;
	}

	public void OnContinue()
	{
		BehaviourSingleton<ScreenNavigation>.instance.LoadMain(showScreen);
	}
}
